package com.example.campus.dao.table_gateway.shiro;


import com.example.campus.dao.model.shiro.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.HashSet;
import java.util.Set;

@Mapper
public interface UserMapper {
    @Insert("insert into t_sys_user " +
            "(login_name, `password`,gender, birthday, phone, email) " +
            "VAlUES " +
            "(#{loginName}, #{password}, #{gender}, #{birthday}, #{phone}, #{email})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addUser(User user);

    @Select("Select * from t_sys_user")
    Set<User> getAllUsers();

    @Select("Select * from t_sys_user where id=#{id}")
    User getUserById(@Param("id") long id);


    @Select("Select * from t_sys_user where login_name=#{loginName}")
    User getUserByLoginName(@Param("loginName") String loginName);

    @Update("<script>" +
            "UPDATE t_sys_user " +
            "<set>" +
            "<if test='loginName != null'>login_name = #{loginName}, </if>"+
            "<if test='password != null'>`password` = #{password}, </if>"+
            "<if test='gender != null'>gender = #{gender}, </if>"+
            "<if test='birthday != null'>birthday = #{birthday}, </if>"+
            "<if test='phone != null'>phone = #{phone}, </if>"+
            "<if test='email != null'>email = #{email} </if>"+
            "</set>"+
            "where id = #{id}"+
            "</script>")
    void updateRoleById(User user);

    @Delete("delete from t_sys_user where id = #{id}")
    void deleteUserById(@Param("id") long id);


    @Results({
//            外键
            @Result(property = "id", column = "id"),
            @Result(property = "loginName", column = "login_name"),
            @Result(property = "password", column = "user_password"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "email", column = "email"),
            @Result(property = "status", column = "status"),
            @Result(property = "roles", javaType = HashSet.class,
                    /*参数, 传给下面的查询方法 */ column = "r_id",
                    many = @Many(select = "com.example.campus.dao.table_gateway.shiro.RoleMapper.getRoleWithPermissionsByRoleId",
                            columnPrefix = "r_", fetchType = FetchType.LAZY))
    })
    @Select("SELECT u.id AS id, u.login_name AS login_name, u.password AS user_password, u.gender AS gender, u.birthday AS birthday, u.phone AS phone, u.email AS email, u.status AS status,   " +
            "r.id AS r_id, r.name AS r_name, r.description  AS r_description " +
            "FROM t_sys_user u " +
            "JOIN t_sys_user_role ur  ON u.id = ur.user_id " +
            "JOIN t_sys_role r  ON ur.role_id = r.id " +
            "where u.id = #{id} ")
    User getUserWithRolesAndPermissions(@Param("id")long id);


    @Results({
//            外键
            @Result(property = "id", column = "id"),
            @Result(property = "loginName", column = "login_name"),
            @Result(property = "password", column = "user_password"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "email", column = "email"),
            @Result(property = "status", column = "status"),
            @Result(property = "roles", javaType = HashSet.class,
                    /*参数, 传给下面的查询方法 */ column = "r_id",
                    many = @Many(select = "com.example.campus.dao.table_gateway.shiro.RoleMapper.getRoleById",
                            columnPrefix = "r_", fetchType = FetchType.LAZY))
    })
    @Select("SELECT u.id AS id, u.login_name AS login_name, u.password AS user_password, u.gender AS gender, u.birthday AS birthday, u.phone AS phone, u.email AS email, u.status AS status,   " +
            "r.id AS r_id, r.name AS r_name, r.description  AS r_description " +
            "FROM t_sys_user u " +
            "JOIN t_sys_user_role ur  ON u.id = ur.user_id " +
            "JOIN t_sys_role r  ON ur.role_id = r.id " +
            "where u.id = #{id} ")
    User getUserWithRoles(@Param("id")long id);
}
